<template>
<view class="content">
	<view class="box">
		<view class="form-title">个人信息</view>
		<view class="form-group" v-if="tableName=='yonghu'">
			<view class="title">账号</view>
			<input disabled="true" class="input" v-model="ruleForm.zhanghao" placeholder="账号" />
		</view>
		<view class="form-group" v-if="tableName=='yonghu'">
			<view class="title">密码</view>
			<input type="password" class="input" v-model="ruleForm.mima" placeholder="密码" />
		</view>
		<view class="form-group" v-if="tableName=='yonghu'">
			<view class="title">昵称</view>
			<input class="input" v-model="ruleForm.nicheng" placeholder="昵称" />
		</view>
		<view class="form-group select" v-if="tableName=='yonghu'">
			<view class="title">性别</view>
			<picker @change="yonghuxingbieChange" :value="yonghuxingbieIndex" :range="yonghuxingbieOptions">
				<view class="picker-select-input">{{ruleForm.xingbie?ruleForm.xingbie:"请选择性别"}}</view>
			</picker>
		</view>
		<view class="form-group" v-if="tableName=='yonghu'">
			<view class="title">手机</view>
			<input class="input" v-model="ruleForm.shouji" placeholder="手机" />
		</view>
		<view class="form-group" v-if="tableName=='yonghu'">
			<view class="title">邮箱</view>
			<input class="input" v-model="ruleForm.youxiang" placeholder="邮箱" />
		</view>
		
		<view class="btn-group">
			<button @tap="update()" class="btn save-btn">保存</button>
			<button @tap="logout()" class="btn logout-btn">退出登录</button>
		</view>
	</view>
</view>
</template>

<script>
	export default {
		data() {
			return {
				ruleForm: {
				},
				tableName:"",
				yonghuxingbieOptions: [],
				yonghuxingbieIndex: 0,
			}
		},
		computed: {
			baseUrl() {
				return this.$base.url;
			}
		},
		async onLoad() {
			let table = uni.getStorageSync("nowTable");
			let res = await this.$api.session(table);
			this.ruleForm = res.data;
			this.tableName = table;
			// 自定义下拉框值
			if(this.tableName=='yonghu'){
				this.yonghuxingbieOptions = "男,女".split(',');
				this.yonghuxingbieOptions.forEach((item, index) => {
					if(item==this.ruleForm.xingbie) {
						this.yonghuxingbieIndex = index;
					}
				});
			}
			this.styleChange()
		},
		methods: {
                        // 下拉变化
                        yonghuxingbieChange(e) {
                                this.yonghuxingbieIndex = e.target.value
                                this.ruleForm.xingbie = this.yonghuxingbieOptions[this.yonghuxingbieIndex]
                        },
			styleChange() {
				this.$nextTick(()=>{
					// document.querySelectorAll('.cu-form-group .uni-input-input').forEach(el=>{
					//   el.style.backgroundColor = this.userInfoForm.list.input.backgroundColor
					// })
				})
			},
			// 获取uuid
			getUUID () {
				return new Date().getTime();
			},
			logout() {
				uni.setStorageSync('token', '');
				this.$utils.jump('../login/login');
			},
			// 注册
			async update() {
				if((!this.ruleForm.zhanghao) && `yonghu` == this.tableName){
					this.$utils.msg(`账号不能为空`);
					return
				}
				if((!this.ruleForm.mima) && `yonghu` == this.tableName){
					this.$utils.msg(`密码不能为空`);
					return
				}
				if(`yonghu` == this.tableName && this.ruleForm.shouji&&(!this.$validate.isMobile(this.ruleForm.shouji))){
					this.$utils.msg(`手机应输入手机格式`);
					return
				}
				if(`yonghu` == this.tableName && this.ruleForm.youxiang&&(!this.$validate.isEmail(this.ruleForm.youxiang))){
					this.$utils.msg(`邮箱应输入邮件格式`);
					return
				}
				let table = uni.getStorageSync("nowTable");
				await this.$api.update(table, this.ruleForm);
				this.$utils.msgBack('修改成功');;
			}

		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f5f7fa;
	}
	
	.content:after {
		position: fixed;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		content: '';
		background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
		background-attachment: fixed;
		background-size: cover;
		background-position: center;
		z-index: -1;
	}

	.box {
		width: 92%;
		margin: 30rpx auto;
		padding: 40rpx 30rpx;
		background-color: #fff;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
	}
	
	.form-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #4A4A4A;
		margin-bottom: 40rpx;
		text-align: center;
	}

	.form-group {
		margin-bottom: 30rpx;
		position: relative;
		
		.title {
			font-size: 28rpx;
			color: #666;
			margin-bottom: 10rpx;
		}
		
		.input {
			width: 100%;
			height: 80rpx;
			background-color: #f8f8f8;
			border-radius: 12rpx;
			padding: 0 20rpx;
			font-size: 28rpx;
			color: #333;
			box-sizing: border-box;
			border: 1px solid #eee;
		}
		
		&.select {
			.picker-select-input {
				width: 100%;
				height: 80rpx;
				background-color: #f8f8f8;
				border-radius: 12rpx;
				padding: 0 20rpx;
				font-size: 28rpx;
				color: #333;
				line-height: 80rpx;
				box-sizing: border-box;
				border: 1px solid #eee;
			}
		}
	}

	.btn-group {
		display: flex;
		justify-content: space-between;
		margin-top: 50rpx;
		
		.btn {
			width: 45%;
			height: 80rpx;
			border-radius: 40rpx;
			font-size: 28rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			
			&.save-btn {
				background-color: #4A4A4A;
				color: #fff;
			}
			
			&.logout-btn {
				background-color: #f5f5f5;
				color: #666;
				border: 1px solid #ddd;
			}
		}
	}
</style>
